package serializable;

import java.io.*;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/14
 */
public class SerialDemo {
    //序列化
    public void serial(){
        try(ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("person.txt"));){
            Person p=new Person("xiaoli",18,"北京");
            //生成一个hash值
            p.setHash();
            oos.writeObject(p);
            oos.flush();
            System.out.println("序列化完成！");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //反序列化
    public void unSerial(){
        try(ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream("person.txt"));) {
            Person person = (Person)ois.readObject();
            System.out.println(person);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    public static void main(String[] args) {
        SerialDemo demo = new SerialDemo();
        demo.serial();
        demo.unSerial();

    }
}
